package cn.com.headfree.evaluation.dao.impl;

import cn.com.headfree.entity.TDeclareEvaluationInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.evaluation.dao.IDeclareEvaluationDao;
import cn.com.headfree.vo.TDeclareEvaluationPointVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.*;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2022/6/28
 **/
@Repository("DeclareEvaluationDao")
public class DeclareEvaluationDaoImpl extends BaseDaoImpl<TDeclareEvaluationPointVo> implements IDeclareEvaluationDao {

    @Override
    public void saveDeclareEvaluationInfo(TDeclareEvaluationInfo DeclareEvaluationInfo) {
        String sql = "insert into t_declare_evaluation_info (id,rule_id,erp_no,user_name,year,declare_type,audit_status,audit_opt,org_id,org_name,creater_id,creater_name,create_time,updater_id,updater_name,update_time,del_sign) values ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{rule_id,jdbcType=VARCHAR},")
                .append("#{erp_no,jdbcType=VARCHAR},").append("#{user_name,jdbcType=VARCHAR},")
                .append("#{year,jdbcType=INTEGER},").append("#{declare_type,jdbcType=VARCHAR},")
                .append("#{audit_status,jdbcType=VARCHAR},")
                .append("#{audit_opt,jdbcType=VARCHAR},").append("#{org_id,jdbcType=VARCHAR},")
                .append("#{org_name,jdbcType=VARCHAR},").append("#{creater_id,jdbcType=VARCHAR},")
                .append("#{creater_name,jdbcType=VARCHAR},").append("#{create_time,jdbcType=DATE},")
                .append("#{updater_id,jdbcType=VARCHAR},").append("#{updater_name,jdbcType=VARCHAR},")
                .append("#{update_time,jdbcType=DATE},").append("#{del_sign,jdbcType=INTEGER})");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", DeclareEvaluationInfo.getId());
        params.put("rule_id", DeclareEvaluationInfo.getRuleId());
        params.put("erp_no", DeclareEvaluationInfo.getErpNo());
        params.put("user_name", DeclareEvaluationInfo.getUserName());
        params.put("year", DeclareEvaluationInfo.getYear());
        params.put("declare_type", DeclareEvaluationInfo.getDeclareType());
        params.put("audit_status", DeclareEvaluationInfo.getAuditStatus());
        params.put("audit_opt", DeclareEvaluationInfo.getAuditOpt());
        params.put("org_id", DeclareEvaluationInfo.getOrgId());
        params.put("org_name", DeclareEvaluationInfo.getOrgName());
        params.put("creater_id", DeclareEvaluationInfo.getCreaterId());
        params.put("creater_name", DeclareEvaluationInfo.getCreaterName());
        params.put("create_time", new Timestamp(new Date().getTime()));
        params.put("updater_id", DeclareEvaluationInfo.getUpdaterId());
        params.put("updater_name", DeclareEvaluationInfo.getUpdaterName());
        params.put("update_time", new Timestamp(new Date().getTime()));
        params.put("del_sign", DeclareEvaluationInfo.getDelSign());
        this.executeSql(sqlBuilder.toString(), params);
    }

    @Override
    public List<TDeclareEvaluationInfo> judge(TDeclareEvaluationInfo tDeclareEvaluationInfo) {
        StringBuilder sqlBuilder = new StringBuilder(
                "select id FROM t_declare_evaluation_info where 1=1 ");
        ArrayList<Object> arrayList = new ArrayList<Object>();
        sqlBuilder.append(" and erp_no=? ");
        arrayList.add(tDeclareEvaluationInfo.getErpNo());
        sqlBuilder.append(" and year=? ");
        arrayList.add(tDeclareEvaluationInfo.getYear());
        return this.findAllBySQL(TDeclareEvaluationInfo.class, sqlBuilder.toString(), null, false, arrayList.toArray());
    }

    @Override
    public void getPageTDeclareEvaluationInfo(Page<TDeclareEvaluationInfo> page, String year, User user) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.* from t_declare_evaluation_info t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" where t.del_sign = 0 ");
        sqlBuilder.append(" and t.erp_no=? ");
        arrayList.add(user.getFax());
        if (!StringUtil.isNullOrEmpty(year)) {
            sqlBuilder.append(" AND t.year= ? ");
            arrayList.add(year);
        }
        sqlBuilder.append(" order by t.create_time desc ");
        this.findPageBySql(TDeclareEvaluationInfo.class, sqlBuilder.toString(), page, arrayList.toArray());
    }

    @Override
    public void updateDeclareEvaluationState(String id, String state,String auditOpt) {
        String sql = "update t_declare_evaluation_info set  ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" audit_status = #{audit_status,jdbcType=VARCHAR}, ");
        sqlBuilder.append(" audit_opt = #{audit_opt,jdbcType=VARCHAR} ");
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and id=#{id,jdbcType=VARCHAR}");
        params.put("audit_status",state);
        params.put("audit_opt",auditOpt);
        params.put("id",id);
        this.executeSql(sqlBuilder.toString(), params);
    }

    @Override
    public void deleteDeclareEvaluationInfo(String DeclareEvaluationId) {
        String sql = "update t_declare_evaluation_info set  ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" del_sign = 1 ");
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and DeclareEvaluationId=#{id,jdbcType=VARCHAR}");
        params.put("DeclareEvaluationId",DeclareEvaluationId);
        this.executeSql(sqlBuilder.toString(), params);
    }

    @Override
    public void getPageDeclareEvaluationInfoQuery(Page<TDeclareEvaluationInfo> page,String baseOrgId, String deptId,String userName, String erpNo, Integer year, String auditStatus) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select distinct TEBI.id AS id,TEBI.NAME AS user_name,TEBI.erp_no AS erp_no,a.year as year,TDER.org_id,case " +
                " when a.erp_no is not null then '已申报' else '未申报' end as audit_status, a.create_time ," +
                " TOD.id AS dept_id,TOD.name AS dept_name ,TOD2.id as org_id,TOD2.name org_name "+
                " FROM t_emp_base_info TEBI " +
                " LEFT JOIN t_dept_emp_rela TDER ON TEBI.erp_no = TDER.erp_no " +
                " LEFT JOIN t_org_dept TOD ON TOD.id = TDER.dept_id " +
                " LEFT JOIN t_org_dept TOD2 ON TOD2.id = TDER.org_id " +
                " left join t_declare_evaluation_info a on a.del_sign = 0  and a.erp_no=TEBI.erp_no and a.audit_status!='2' " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        if (null!=year) {
            sqlBuilder.append(" AND a.year =? ");
            arrayList.add(year);
        }
        sqlBuilder.append(" WHERE TEBI.del_sign = 0 AND TEBI.is_valid = 0 ");
        sqlBuilder.append(" AND TOD2.path LIKE concat('%',?,'%') ");
        arrayList.add(baseOrgId);
        if (StringUtils.isNotBlank(deptId)) {
            sqlBuilder.append(" AND TOD.path LIKE concat('%',?,'%') ");
            arrayList.add(deptId);
        }
        if (StringUtils.isNotBlank(userName)) {
            sqlBuilder.append(" AND TEBI.NAME LIKE concat('%',?,'%') ");
            arrayList.add(userName);
        }
        if (StringUtils.isNotBlank(erpNo)) {
            sqlBuilder.append(" AND TEBI.erp_no LIKE concat('%',?,'%') ");
            arrayList.add(erpNo);
        }
        if (StringUtils.isNotBlank(auditStatus)) {
            if(auditStatus.equals("0")){
                //已申报
                sqlBuilder.append(" AND a.erp_no is not null ");
            }else{
                //未申报
                sqlBuilder.append(" AND a.erp_no is null ");
            }
        }
        sqlBuilder.append(" order by a.year desc,a.erp_no desc,TOD.id ");
        this.findPageBySql(TDeclareEvaluationInfo.class, sqlBuilder.toString(), page, arrayList.toArray());
    }

    @Override
    public List<TDeclareEvaluationInfo> getListSalaryInfoQuery(String baseOrgId, String deptId,String userName, String erpNo, Integer year, String auditStatus) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select distinct TEBI.id AS id,TEBI.NAME AS user_name,TEBI.erp_no AS erp_no,a.year as year,TDER.org_id,case " +
                " when a.erp_no is not null then '已申报' else '未申报' end as audit_status, a.create_time ," +
                " TOD.id AS dept_id,TOD.name AS dept_name ,TOD2.id as org_id,TOD2.name org_name "+
                " FROM t_emp_base_info TEBI " +
                " LEFT JOIN t_dept_emp_rela TDER ON TEBI.erp_no = TDER.erp_no " +
                " LEFT JOIN t_org_dept TOD ON TOD.id = TDER.dept_id " +
                " LEFT JOIN t_org_dept TOD2 ON TOD2.id = TDER.org_id " +
                " left join t_declare_evaluation_info a on a.del_sign = 0  and a.erp_no=TEBI.erp_no and a.audit_status!='2' " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        if (null!=year) {
            sqlBuilder.append(" AND a.year =? ");
            arrayList.add(year);
        }
        sqlBuilder.append(" WHERE TEBI.del_sign = 0 AND TEBI.is_valid = 0 ");
        sqlBuilder.append(" AND TOD2.path LIKE concat('%',?,'%') ");
        arrayList.add(baseOrgId);
        if (StringUtils.isNotBlank(deptId)) {
            sqlBuilder.append(" AND TOD.path LIKE concat('%',?,'%') ");
            arrayList.add(deptId);
        }
        if (StringUtils.isNotBlank(userName)) {
            sqlBuilder.append(" AND TEBI.NAME LIKE concat('%',?,'%') ");
            arrayList.add(userName);
        }
        if (StringUtils.isNotBlank(erpNo)) {
            sqlBuilder.append(" AND TEBI.erp_no LIKE concat('%',?,'%') ");
            arrayList.add(erpNo);
        }
        if (StringUtils.isNotBlank(auditStatus)) {
            if(auditStatus.equals("0")){
                //已申报
                sqlBuilder.append(" AND a.erp_no is not null ");
            }else{
                //未申报
                sqlBuilder.append(" AND a.erp_no is null ");
            }
        }
        sqlBuilder.append(" order by a.year desc,a.erp_no desc,TOD.id ");
        return this.findAllBySQL(TDeclareEvaluationInfo.class, sqlBuilder.toString(),null,false,arrayList.toArray());
    }


    @Override
    public TDeclareEvaluationInfo getTDeclareEvaluationInfoById(String id){
        StringBuilder sqlBuilder = new StringBuilder(
                "select t.* FROM t_declare_evaluation_info t");
        sqlBuilder.append(" where t.id=? ");
        ArrayList<Object> arrayList = new ArrayList<Object>();
        arrayList.add(id);
        return this.findUnique(TDeclareEvaluationInfo.class, sqlBuilder.toString(), arrayList.toArray());
    }
}
